package com.web.music.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.web.music.model.Album;
import com.web.music.model.Artist;
import com.web.music.model.Catalog;
import com.web.music.model.Genre;
import com.web.music.model.Person;
import com.web.music.model.Track;

/**
 * Loads XML files from local file system
 */
public class LocalMusicProcessor extends BaseMusicProcessor {

	String personsFilePath;
	String catalogFilePath;

	public void execute(){
		super.execute();
		Map<String,Person> persons = getXmlParser().extractPersons(new File(personsFilePath));
		
		getPersistence().saveAll(new ArrayList<Person>(persons.values()));
		
		Catalog catalog = getXmlParser().extractCatalog(new File(catalogFilePath), persons);
		
		getPersistence().saveAll(new ArrayList<Genre>(catalog.getGenres()));
		
		getPersistence().saveAll(new ArrayList<Artist>(catalog.getArtists()));
		
		getPersistence().saveAll(new ArrayList<Album>(catalog.getAlbums()));
		
		List<Track> tracks = new ArrayList();
		
		Track t1 = new Track();
	
		
		t1.setAlbum(null);
		t1.setContent(null);
		t1.setName("name");
		t1.setNum(2);
		t1.setId(1);	
		

		byte[] newfile = new byte[1000000];
		
		try {
			FileInputStream mp3 = new FileInputStream("squirell1.gif");	
			
			
			mp3.read(newfile);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		t1.setContent(newfile);
		
		tracks.add(t1);
		
		getPersistence().saveAll(tracks);
	}
	
	public String getPersonsFilePath() {
		return personsFilePath;
	}

	public void setPersonsFilePath(String personsFilePath) {
		this.personsFilePath = personsFilePath;
	}

	public String getCatalogFilePath() {
		return catalogFilePath;
	}

	public void setCatalogFilePath(String catalogFilePath) {
		this.catalogFilePath = catalogFilePath;
	}
	
}
